package ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import org.impl.UserDAOProxy;
import org.vo.UserVo;


public class Register {
	 private JFrame frame=new JFrame("注册");
	 private JLabel lab1=new JLabel("ID",JLabel.RIGHT);
	 private JLabel lab2=new JLabel("姓名",JLabel.RIGHT);
	 private JLabel lab3=new JLabel("密码",JLabel.RIGHT);
	 private JLabel lab4=new JLabel("性别",JLabel.RIGHT);
	 private JLabel lab5=new JLabel("确认密码",JLabel.RIGHT);
	 private JLabel error1=new JLabel("两个密码不一样，请重新输入！");
	 private JLabel error2=new JLabel("密码不能为空!");
	 private JLabel error3=new JLabel("该ID已存在，请重新选择ID!");
	 private JTextField name=new JTextField();
	 private JPasswordField password=new JPasswordField();
	 private JTextField id=new JTextField();
	 private JPasswordField password2=new JPasswordField();
	 private String s[]={"男","女"};
	 private JComboBox sex=new  JComboBox(s);
	 private JButton but=new JButton("注册");
	 
	public Register() {
		// TODO Auto-generated constructor stub
		
				lab1.setBounds(40, 58, 50,27);
				id.setBounds(100, 58, 150, 27);
				lab2.setBounds(40, 100, 50,27);
				name.setBounds(100, 100, 150, 27);
				lab3.setBounds(40, 142, 50,27);
				password.setBounds(100,142,150,27);
				lab4.setBounds(250, 140, 50,27);
				sex.setBounds(310,140,60,30);
				but.setBounds(310,180,60,30);
				lab5.setBounds(10, 184, 80,27);
				password2.setBounds(100,184,150,27);
				error1.setBounds(100, 214,250, 27);
				error2.setBounds(100, 214,250, 27);
				error3.setBounds(100, 214,250, 27);
				frame.add(id);
				frame.add(name);
				frame.add(password);
				frame.add(sex);
				frame.add(password2);
				frame.add(but);
				frame.add(lab1);
				frame.add(lab2);
				frame.add(lab3);
				frame.add(lab4);
				frame.add(lab5);
				frame.add(error1);
				frame.add(error2);
				error1.setVisible(false);
				frame.add(error3);
				error2.setVisible(false);
				error3.setVisible(false);
				frame.setVisible(true);
				frame.setSize(430,300);
				frame.setLayout(null);
				frame.setLocationRelativeTo(null);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				but.addActionListener(new ActionListener(){
 
					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
				
						if("".equals(password.getText())){
							error1.setVisible(false);
							  error3.setVisible(false);
							error2.setVisible(true);  //密码为空显示错误
							
							
							 }
						else if(!password.getText().equals(password2.getText())){
						    error2.setVisible(false);  
						    error3.setVisible(false);  
							error1.setVisible(true);            //密码和确认密码不同显示错误
						     
						}
						
						else{
							UserVo user=new UserVo();
							user.setId(id.getText());
							user.setUserName(name.getText());
							user.setPassword(password.getText());
							user.setSex((String)sex.getSelectedItem());
						 try {
							if(new UserDAOProxy().create(user)==true)        //注册成功，提示是否跳转到登录界面
							{
								int answer=JOptionPane.showConfirmDialog(null,"注册成功，是否返回登录界面？");
								if(answer==0)  
									{ frame.dispose();
									 new Login();
									}
									
							}
							else                                          //注册失败，显示错误信息
							{       error1.setVisible(false); 
								    error2.setVisible(false);  
								    error3.setVisible(true);  
									
							}
							
								
							                                              //创造用户
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						}
					}
					
				});
				
			
			
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
  new Register();
	}

}
